package com.goddess.yeecolor.view;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.ToastUtils;

import java.io.File;
import java.io.FileOutputStream;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享
 */
public class sharePopuWindow extends PopupWindow{
    private View mView;
    private LinearLayout wx,wxfrend,qq;
    private Button share_quxiao;
    private String tag,jump;
    public sharePopuWindow(Activity activity,String jump,String tag){
        mView= LayoutInflater.from(activity).inflate(R.layout.popuwidow_share,null);
        wx= (LinearLayout) mView.findViewById(R.id.sharewxLinear);
        wxfrend= (LinearLayout) mView.findViewById(R.id.sharewxFrendLinear);
        qq= (LinearLayout) mView.findViewById(R.id.shareqqLinear);
        share_quxiao= (Button) mView.findViewById(R.id.share_quxiao);
        this.setContentView(mView);
        //设置SelectPicPopupWindow的View
        this.setContentView(mView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.take_photo_anim);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //显示位置
        this.tag=tag;
        this.jump=jump;
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mView.setOnTouchListener(( v, event)->{
            int height = mView.findViewById(R.id.pop_layout).getTop();
            int y=(int) event.getY();
            if(event.getAction()== MotionEvent.ACTION_UP){
                if(y<height){
                    dismiss();
                }
            }
            return true;

        });
        //取消按钮
        share_quxiao.setOnClickListener((v)->{
            //销毁弹出框
            dismiss();

        });
        /**
         * 微信朋友圈
         */
        wxfrend.setOnClickListener((view)->{
            setShare(activity,Wechat.NAME);
        });
        wx.setOnClickListener((view)->{

            setShare(activity,WechatMoments.NAME);
        });
        qq.setOnClickListener((view)->{
            setShare(activity,QQ.NAME);
        });
    }

    private void setShare(Activity context,String name){
        //设置图片路径
        Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),R.mipmap.icon);
        File file=new File(Environment.getExternalStorageDirectory(),"Goddess/image");

        if(!file.exists()){
            file.mkdirs();
        }
        File filse=new File(file,"icon.png");
        FileOutputStream outputStream=null;
        File sd = Environment.getExternalStorageDirectory();
        boolean can_write = sd.canWrite();

        try {
            outputStream=new FileOutputStream(filse);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
            outputStream.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        setWecharShare(name,context);
    }
    private void setWecharShare(String name,Activity context)
    {
        Platform platform = ShareSDK.getPlatform(context,name);
        if (name.equals(QQ.NAME)){
            if (platform.isClientValid()){
                QQ.ShareParams params=new QQ.ShareParams();
                if (jump.equals(MyUrl.TYPE)){//活動
                    params.setTitleUrl(MyUrl.SHAREACTIVITY+tag);
                    params.setUrl(MyUrl.SHAREACTIVITY+tag);
                }
                else if (jump.equals(MyUrl.TYPEC)){//學院
                    params.setTitleUrl(MyUrl.SHARETYPEC+tag);
                    params.setUrl(MyUrl.SHARETYPEC+tag);
                }
                else {
                    params.setTitleUrl(MyUrl.SHAREJOIN+tag);
                    params.setUrl(MyUrl.SHAREJOIN+tag);
                }

                params.setTitle("优雅荟");
                params.setText("分享");
                params.setImagePath("/storage/emulated/0/Goddess/image/icon.png");
                params.setShareType(Platform.SHARE_WEBPAGE);
                platform.share(params);
            }
            else {
                ToastUtils.toast("没有安装QQ客户端");
            }
        }
        else {
            if (platform.isClientValid()){
                Wechat.ShareParams sp = new Wechat.ShareParams();

                sp.setTitle("优雅荟");
                if (jump.equals(MyUrl.TYPE)){//活動
                    sp.setUrl(MyUrl.SHAREACTIVITY+tag);
                }
                else if (jump.equals(MyUrl.TYPEC)){//學院
                    sp.setUrl(MyUrl.SHARETYPEC+tag);
                }
                else {
                    sp.setUrl(MyUrl.SHAREJOIN+tag);
                }
                sp.setText("分享");
                sp.setImagePath("/storage/emulated/0/Goddess/image/icon.png");
                sp.setShareType(Platform.SHARE_WEBPAGE);
                platform.share(sp);
            }
            else {
                ToastUtils.toast("没有安装微信客户端");
            }

        }
    }

}
